package pi.client.units;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import pi.client.core.MyProgram;
import pi.client.players.Player;

public class UnitBase {
	static public final int UnitTypeKing = 1;
	static public final int UnitTypeSoldier = 2;
	static public final int UnitTypeSoldierArcher = 3;

	public static int uniqueIdCount = 1;

	protected MyProgram program;

	public BufferedImage image;

	public int x;
	public int y;
	public int width;
	public int height;

	public int life;
	public int attack;
	public int movimentation;
	public int range;
	public boolean isAlive;

	public int type;
	public int uniqueId;

	public boolean isToDrawAttackAnimation;
	public boolean isToDrawDamageReceivedAnimation;
	private int alphaTimer;
	private int alphaTimerCount;

	public UnitBase() {
		this.program = null;

		this.image = null;

		this.x = -1;
		this.y = -1;
		this.width = -1;
		this.height = -1;

		this.life = -1;
		this.attack = -1;
		this.movimentation = -1;
		this.range = -1;
		this.isAlive = false;

		this.type = -1;
		this.uniqueId = -1;

		this.isToDrawAttackAnimation = false;
		this.isToDrawDamageReceivedAnimation = false;
		this.alphaTimer = 3 * program.game.second;
		this.alphaTimerCount = 0;
	}

	public UnitBase(MyProgram program, BufferedImage image, int x, int y,
			int life, int attack, int movimentation, int range, int type) {
		this.program = program;

		this.image = image;

		this.x = x;// * program.mainPanel.board.cellWidth;
		this.y = y;// * program.mainPanel.board.cellHeight;
		// this.width = image.getWidth();
		// this.height = image.getHeight();
		this.width = program.mainPanel.board.cellWidth;
		this.height = program.mainPanel.board.cellHeight;

		this.life = life;
		this.attack = attack;
		this.movimentation = movimentation;
		this.range = range;
		this.isAlive = true;

		this.type = type;
		this.uniqueId = uniqueIdCount;
		uniqueIdCount++;
	}

	public void Update(long timer) {
		if (life <= 0){
			isAlive = false;
		}
		
		if (isAlive == true) {
//			if (isToDrawAttackAnimation == true) {
//				alphaTimerCount += timer;
				if (alphaTimerCount >= alphaTimer) {
//					alphaTimerCount = 0;
//					isToDrawAttackAnimation = false;
//					isToDrawDamageReceivedAnimation = false;
//				}
			}
		}
	}

	private int GetAnimationAlpha() {
		int alpha = 128;// alphaTimerCount/255;
		return alpha;
	}
	
	public void StartAnimationAttack(){
		if(isToDrawAttackAnimation == true){
			isToDrawAttackAnimation = false;
		}else{
			isToDrawAttackAnimation = true;
		}
	}

	public void Draw(Graphics2D g) {
		if (isAlive == true) {
			int realX = program.mainPanel.board.cellWidth * x;
			int realY = program.mainPanel.board.cellHeight * y;

			g.drawImage(image, realX, realY, program.mainPanel.board.cellWidth,
					program.mainPanel.board.cellHeight, null);

			Player me = program.mainPanel.playerMe;
			Player enemy = program.mainPanel.playerEnemy;

			if (me.CheckIfIsMyUnit(this)) {
				g.setColor(new Color(0, 255, 0, 64));
			} else if (enemy.CheckIfIsMyUnit(this)) {
				g.setColor(new Color(255, 0, 0, 64));
			}
			g.fillRect(realX, realY, width, height);

			if (isToDrawAttackAnimation == true) {
				AttackAnimation(g, GetAnimationAlpha(), realX, realY);
				// AttackAnimation(g, 255, realX, realY);
			} else if (isToDrawDamageReceivedAnimation == true) {

			}
		}
	}

	public void AttackAnimation(Graphics2D g, int alphaTimer, int xx, int yy) {
		g.setColor(new Color(0, 0, 255, 255));//alphaTimer));
		g.fillRect(xx, yy, width, height);
		g.setColor(Color.white);
		g.drawString("Attacking!", xx+5, yy + program.mainPanel.board.cellHeight/2);

	}

	public void DamageReceivedAnimation(Graphics2D g) {

	}

	public String GetStringUnitType() {
		switch (type) {
		case UnitTypeKing:
			return "King";

		case UnitTypeSoldier:
			return "Soldier";

		case UnitTypeSoldierArcher:
			return "Archer";
		}
		return null;
	}

	public void ShowData() {
//		if (type == UnitTypeKing) {
//			System.out.println("Id: UnitTypeKing          - ");
//		} else if (type == UnitTypeSoldier) {
//			System.out.println("Id: UnitTypeSoldier       - ");
//		} else if (type == UnitTypeSoldierArcher) {
//			System.out.println("Id: UnitTypeSoldierArcher - ");
//		}

		System.out.println("X: " + x);
		System.out.println("Y: " + y);
		// System.out.println("Width: " + width);
		// System.out.println("height: " + height);
		//
		System.out.println("Life: " + life);
		// System.out.println("Attack: " + attack);
		// System.out.println("Movimentation: " + movimentation);
		// System.out.println("Is Alive: " + isAlive);
		//
		// System.out.println("Unique Id: " + uniqueId);
	}
}